package com.financing.platform.service.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.platform.common.BizEnums;
import com.financing.platform.dao.IYouDanAmountDao;
import com.financing.platform.dto.BankCardDTO;
import com.financing.platform.dto.FpPaymentDTO;
import com.financing.platform.dto.WallettWithdrawDTO;
import com.financing.platform.dto.YouDanAmountDTO;
import com.financing.platform.service.IPaymentService;
import com.financing.platform.service.IYouDanAmountService;
import com.financing.platform.vo.WallettWithdrawVO;

/**
 * 有单金额明细相关
 * @author cuimo
 *
 */
@Service("youDanAmountServiceImpl")
@Transactional
public class YouDanAmountServiceImpl extends BaseServiceImpl implements IYouDanAmountService {

	
	@Autowired
	private IYouDanAmountDao youDanAmountDaoImpl;
	
	@Autowired
	private IPaymentService paymentServiceImpl;

	@Override
	public int updateYouDanAmount(YouDanAmountDTO youDanAmount) throws SQLException {
		return youDanAmountDaoImpl.updateYouDanAmount(youDanAmount);
	}

	@Override
	public Integer insertYouDanAmount(YouDanAmountDTO youDanAmount) throws SQLException {
		return youDanAmountDaoImpl.insertYouDanAmount(youDanAmount);
	}
	
	@Override
	public YouDanAmountDTO getYouDanAmount(YouDanAmountDTO youDanAmount) throws SQLException {
		return youDanAmountDaoImpl.getYouDanAmount(youDanAmount);
	}
	
	@Override
	public int updateBaoZhengJin2Lock(String receiptId, String remark) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("01");
		youDanAmount.setAssociationId(receiptId);
		youDanAmount.setOperateType("01");
		youDanAmount.setRemark(remark);
		return this.updateYouDanAmount(youDanAmount);
	}

	@Override
	public int updateBaoZhengJin2Unlock(String receiptId, String remark) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("01");
		youDanAmount.setAssociationId(receiptId);
		youDanAmount.setOperateType("02");
		youDanAmount.setRemark(remark);
		return this.updateYouDanAmount(youDanAmount);
	}

	@Override
	public int updateBaoZhengJin2Withdraw(String receiptId, String remark) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("01");
		youDanAmount.setAssociationId(receiptId);
		youDanAmount.setOperateType("03");
		youDanAmount.setRemark(remark);
		return this.updateYouDanAmount(youDanAmount);
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public int updateBaoZhengJin2WithdrawSuccess(String receiptId, String remark, String platSerialNo) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("01");
		youDanAmount.setAssociationId(receiptId);
		youDanAmount.setOperateType("04");
		youDanAmount.setRemark(remark);
		
		
		//更新 支付流水状态
		FpPaymentDTO paymentDTO = new FpPaymentDTO();
		paymentDTO.setPayStatus(BizEnums.PayEnums.PAY_STATUS_SUCC);
		paymentDTO.setTpayRetMsg("支付成功");
		paymentDTO.setPayEndTime(new Date());//支付结束时间
		paymentServiceImpl.updateByAmountDetail(platSerialNo, paymentDTO);
		return this.updateYouDanAmount(youDanAmount);
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public int updateBaoZhengJin2WithdrawFail(String receiptId, String remark, String platSerialNo) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("01");
		youDanAmount.setAssociationId(receiptId);
		youDanAmount.setOperateType("05");
		youDanAmount.setRemark(remark);
		
		
		/* 	
		paymentUp.setPlatSerialNo(BasicUtils.genPlatSerialNoSimple(
				BizEnums.PayEnums.PAY_3PLAT_BAOFOO));//平台流水号(加上支付商标识)
		paymentUp.setTpay3plat(BizEnums.PayEnums.PAY_3PLAT_BAOFOO);//宝付支付
		paymentUp.setTpayReSerialNo(baofoorsp.getTrans_no());//宝付订单流水号
		paymentUp.setPayEndTime(new Date());//支付结束时间
		paymentUp.setTpayResult(baofoorsp.getResp_msg());//处理结果
		paymentUp.setTpayRetCode(baofoorsp.getResp_code());//支付错误码
		paymentUp.setTpayRetMsg(baofoorsp.getResp_msg());//支付错误信息
		*/
		//更新 支付流水状态
		FpPaymentDTO paymentDTO = new FpPaymentDTO();
		paymentDTO.setPayStatus(BizEnums.PayEnums.PAY_STATUS_FAIL);
		paymentDTO.setPayEndTime(new Date());//支付结束时间
		paymentDTO.setTpayRetMsg(remark);
		paymentServiceImpl.updateByAmountDetail(platSerialNo, paymentDTO);
		
		return this.updateYouDanAmount(youDanAmount);
	}
	
	@Override
	public int updateBaoZhengJin2Fail(String receiptId, String remark) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("01");
		youDanAmount.setAssociationId(receiptId);
		youDanAmount.setOperateType("06");
		youDanAmount.setRemark(remark);
		return this.updateYouDanAmount(youDanAmount);
	}


	@Override
	public int updateYongJin2Lock(String orderId, String remark) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("02");
		youDanAmount.setAssociationId(orderId);
		youDanAmount.setOperateType("01");
		youDanAmount.setRemark(remark);
		return this.updateYouDanAmount(youDanAmount);
	}

	@Override
	public int updateYongJin2Unlock(String orderId, String remark) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("02");
		youDanAmount.setAssociationId(orderId);
		youDanAmount.setOperateType("02");
		youDanAmount.setRemark(remark);
		return this.updateYouDanAmount(youDanAmount);
	}

	@Override
	public int updateYongJin2Withdraw(String orderId, String remark) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("02");
		youDanAmount.setAssociationId(orderId);
		youDanAmount.setOperateType("03");
		youDanAmount.setRemark(remark);
		return this.updateYouDanAmount(youDanAmount);
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public int updateYongJin2WithdrawSuccess(YouDanAmountDTO youDanAmount, String remark, String platSerialNo) throws SQLException {
		YouDanAmountDTO youDanAmountTemp = new YouDanAmountDTO();
		youDanAmountTemp.setAmountType(youDanAmount.getAmountType());
		youDanAmountTemp.setAssociationId(youDanAmount.getAssociationId());
		youDanAmountTemp.setOperateType("04");
		youDanAmountTemp.setRemark(remark);
		
		//更新 支付流水状态
		FpPaymentDTO paymentDTO = new FpPaymentDTO();
		paymentDTO.setPayStatus(BizEnums.PayEnums.PAY_STATUS_SUCC);
		paymentDTO.setTpayRetMsg("支付成功");
		paymentDTO.setPayEndTime(new Date());//支付结束时间
		paymentServiceImpl.updateByAmountDetail(platSerialNo, paymentDTO);
		
		return this.updateYouDanAmount(youDanAmountTemp);
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public int updateYongJin2WithdrawFail(String orderId, String remark, String platSerialNo) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("02");
		youDanAmount.setAssociationId(orderId);
		youDanAmount.setOperateType("05");
		youDanAmount.setRemark(remark);
		
		//更新 支付流水状态
		FpPaymentDTO paymentDTO = new FpPaymentDTO();
		paymentDTO.setPayStatus(BizEnums.PayEnums.PAY_STATUS_FAIL);
		paymentDTO.setTpayRetMsg(remark);
		paymentDTO.setPayEndTime(new Date());//支付结束时间
		paymentServiceImpl.updateByAmountDetail(platSerialNo, paymentDTO);
		
		return this.updateYouDanAmount(youDanAmount);
	}

	@Override
	public int updateYongJin2Fail(String orderId, String remark) throws SQLException {
		YouDanAmountDTO youDanAmount = new YouDanAmountDTO();
		youDanAmount.setAmountType("02");
		youDanAmount.setAssociationId(orderId);
		youDanAmount.setOperateType("06");
		youDanAmount.setRemark(remark);
		return this.updateYouDanAmount(youDanAmount);
	}
	@Override
	public Integer getYouDanWithdrawListCount(WallettWithdrawVO wallettWithdrawVO) throws SQLException{
		return youDanAmountDaoImpl.getYouDanWithdrawListCount(wallettWithdrawVO);
	}

	@Override
	public List<WallettWithdrawDTO> getYouDanWithdrawList(WallettWithdrawVO wallettWithdrawVO) throws SQLException {
		return youDanAmountDaoImpl.getYouDanWithdrawList(wallettWithdrawVO);
	}

	@Override
	public BankCardDTO queryFaUserBankInfo(WallettWithdrawDTO wallettWithdrawDTO) throws SQLException {
		return youDanAmountDaoImpl.queryFaUserBankInfo(wallettWithdrawDTO);
	}

	@Override
	public List<YouDanAmountDTO> getCommonOrderAmountByAssociationId(String id) throws SQLException {
		return youDanAmountDaoImpl.getCommonOrderAmountByAssociationId(id);
	}

}
